package com.ouken.activity;




import db_ctr.db_RW;
import device_ctr.Dev_logic;
import device_ctr.Dev_para;
import device_ctr.Wifi_ctronller;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class EditDevice12Activity extends Activity {
	private Context context;
	private ImageButton back2;
	private ImageButton back1;
	private ImageButton button2;
	private EditText text2;
	private EditText text3;
	private String deviceSSID;
	private String deviceBSSID;
	private String devicePWD;
	private String deviceDescription;
	private void initView(){
		context = this;
		back1 = (ImageButton)findViewById(R.id.edit_device12_back1); 
		back2 = (ImageButton)findViewById(R.id.edit_device12_back2); 
		button2 = (ImageButton)findViewById(R.id.edit_device12_button2); 
		text2 = (EditText)findViewById(R.id.edit_device12_edittext_text2); 
		text3 = (EditText)findViewById(R.id.edit_device12_edittext_text3); 	
		
		Intent intent = getIntent(); 
		devicePWD = intent.getStringExtra("devicePWD").toString();
		deviceSSID = intent.getStringExtra("deviceSSID").toString();
		//deviceBSSID= intent.getStringExtra("deviceBSSID").toString();
		deviceDescription= intent.getStringExtra("deviceDescription").toString();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_device12);
		initView();
		text2.setText(deviceDescription);
		back1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditDevice12Activity.this.finish();
			}
		});
		
		back2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditDevice12Activity.this.finish();
			}
		});
		
		button2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String checkResult = "OK";
		        int errorMessage = 0  ;
		        if(text2.getText().length() == 0){
		        	checkResult = "ERROR";
		        	errorMessage = R.string.stringtext2error;
		        }
		        else{
			        /******************************************************/
			        //濡傛灉闇�鏇存柊鐨勫瘑纰肩殑鏅傚�锛岄�琛屼笅鍒楅璀�
			        if (text3.getText().toString().length()>0){
			        	if (text3.getText().toString().matches("[0-9A-Za-z]*")==false || text3.getText().toString().length()>9){
			        		checkResult = "ERROR";
				        	errorMessage = R.string.passwd_format_error;
			        	}
			        	else{
			        		Dev_logic logic_er=new Dev_logic();
							String result=logic_er.change_device_pwd(devicePWD,text3.getText().toString() );
							Log.e("Duan", result);
							if (result.startsWith("OK")==false){
								checkResult = "ERROR";
					        	errorMessage = R.string.server_no_answer;
							}
							else{
								devicePWD=text3.getText().toString();
							}
			        	}
				
			        }
			        /******************************************************/
		        }
		        if(checkResult.equals("ERROR")){
		        	AlertDialog myDialog = new AlertDialog.Builder(EditDevice12Activity.this).
		        	setTitle(R.string.error).
		        	setMessage(errorMessage).
		        	setPositiveButton(R.string.confirm, null).create();
		        	myDialog.show();
					return;
					
		        }
		        /******************************************************/
		        //BSSID信息獲得
		        Wifi_ctronller wifi_reader=new Wifi_ctronller(context);
		        deviceBSSID= wifi_reader.getBSSID();
		          //娣诲姞瑷倷code
		        db_RW kk=new db_RW(context);
				Dev_para jj=new Dev_para(deviceBSSID,deviceSSID,devicePWD,1,getString(R.string.device_name_01),text2.getText().toString(),1,0);
				kk.add_device(jj);
		        /******************************************************/
		        
		        AlertDialog myDialog = new AlertDialog.Builder(EditDevice12Activity.this).
			        	setMessage(R.string.process_sucessed).
			        	setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								EditDevice12Activity.this.finish();
							}
						}).create();
			        	myDialog.show();
			      
				return;
		        
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

}
